#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <list>
#include <cstdlib>

using namespace std;

/*
 * We use the stringstream to "tokenize" the file. 
 * We then output each seperate number from the string stream using a >>
 * When we do this, use atoi() to change into int.
 * Then, push onto a vector or linked list data structure
 * Do this on the other intermediary file as well.
 * Then, sort the data structure using a sorting algorithm.
 * */

void s2u(string &old) //turn spaces into nothing
{
	int prev = 0;
	int prev2 = 0;
	while(1)
	{
		prev = old.find(',',0); //need to change commas...
		if(prev != string::npos)
			old.replace(prev, 1," "); //to spaces
		else
			break;
	}
	while (1)
	{
		prev2 = old.find('\n', 0);
		if(prev2 != string::npos)
			old.replace(prev2, 1, "\0");
		else
			break;
	}
}

int main()
{	
	int size = 0;
	list<int> list1;

	stringstream sstream1;
	ifstream f1("data.in");
	sstream1 << f1.rdbuf();
	string file1 = sstream1.str();
	s2u(file1);
	
	stringstream a1;
	a1 << file1;
	
	while(!(a1.eof()))
	{
		int currentint;
		string current;
		a1 >> current;
		currentint = atoi((char*)current.c_str());
		list1.push_back(currentint);

		size++;
	}
	list1.sort();
	
	list<int>::iterator i;
	for(i=list1.begin(); i != list1.end(); i++) 
		cout << *i << " ";
	f1.close();
	cout << endl << endl << "The number of integers is: " << size << endl;
}
